#!/bin/bash
verif_ptrs() {
    porta=$1
    PT=$(lsof -V -i tcp -P -n | grep -v "ESTABLISHED" | grep -v "COMMAND" | grep "LISTEN")
    for pton in $(echo -e "$PT" | cut -d: -f2 | cut -d' ' -f1 | uniq); do
        svcs=$(echo -e "$PT" | grep -w "$pton" | awk '{print $1}' | uniq)
        [[ "$porta" = "$pton" ]] && {
            echo -e "\n\033[1;31mPORTA \033[1;33m$porta \033[1;31mEM USO PELO \033[1;37m$svcs\033[0m"
            sleep 3
            fun_initcheck
        }
    done
}

fun_bar() {
    comando[0]="$1"
    comando[1]="$2"
    (
        [[ -e $HOME/fim ]] && rm $HOME/fim
        [[ ! -d /etc/SSHPlus ]] && rm -rf /bin/menu
        ${comando[0]} >/dev/null 2>&1
        ${comando[1]} >/dev/null 2>&1
        touch $HOME/fim
    ) >/dev/null 2>&1 &
    tput civis
    echo -ne "\033[1;33mAGUARDE \033[1;37m- \033[1;33m["
    while true; do
        for ((i = 0; i < 18; i++)); do
            echo -ne "\033[1;31m#"
            sleep 0.1s
        done
        [[ -e $HOME/fim ]] && rm $HOME/fim && break
        echo -e "\033[1;33m]"
        sleep 1s
        tput cuu1
        tput dl1
        echo -ne "\033[1;33mAGUARDE \033[1;37m- \033[1;33m["
    done
    echo -e "\033[1;33m]\033[1;37m -\033[1;32m OK !\033[1;37m"
    tput cnorm
}

fun_initcheck() {
    clear
    echo -e "\E[44;1;37m            GERENCIAR CHECKUSER             \E[0m"
    echo ""
    var_sks1=$(ps x | grep "checkuser" | grep -v grep >/dev/null && echo -e "\033[1;32m◉ " || echo -e "\033[1;31m○ ")
    var_sks2=$(ps x | grep "4gcheck" | grep -v grep >/dev/null && echo -e "\033[1;32m◉ " || echo -e "\033[1;31m○ ")
    echo ""
    echo -e "\033[1;31m[\033[1;36m1\033[1;31m] \033[1;37m• \033[1;33mATIVAR CHECKUSER(PADRÃO) $var_sks1 \033[0m"
    echo -e "\033[1;31m[\033[1;36m2\033[1;31m] \033[1;37m• \033[1;33mATIVAR CHECKUSER(CONECTA4G) $var_sks2 \033[0m"
    echo -e "\033[1;31m[\033[1;36m0\033[1;31m] \033[1;37m• \033[1;33mVOLTAR\033[0m"
    echo ""
    echo -ne "\033[1;32mO QUE DESEJA FAZER \033[1;33m?\033[1;37m "
    read resposta
    if [[ "$resposta" = '1' ]]; then
        if ps x | grep -w checkuser | grep -v grep 1>/dev/null 2>/dev/null; then
            clear
            echo -e "\E[41;1;37m             CHECKUSER(PADRÃO)              \E[0m"
            echo ""
            fun_stopbad() {
                screen -r -S "checkuser" -X quit
                [[ $(grep -wc "check.py" /etc/autostart) != '0' ]] && {
                    sed -i '/check.py/d' /etc/autostart
                }
                sleep 1
                screen -wipe >/dev/null
            }
            echo -e "\033[1;32mDESATIVANDO O CHECKUSER(PADRÃO)\033[1;33m"
            echo ""
            fun_stopbad
            echo ""
            echo -e "\033[1;32mCHECKUSER(PADRÃO) DESATIVADO COM SUCESSO!\033[1;33m"
            sleep 3
            fun_initcheck
        else
            clear
            echo -e "\E[44;1;37m            ATIVAÇÃO DO CHECKUSER(PADRÃO)             \E[0m"
            echo ""
            echo -ne "\033[1;32mQUAL PORTA DESEJA ULTILIZAR \033[1;33m?\033[1;37m: "
            read porta
            [[ $porta != ?(+|-)+([0-9]) ]] && {
                echo ""
                echo -e "\033[1;31mPorta invalida!"
                sleep 3
                clear
                fun_initcheck
            }
            verif_ptrs $porta
            fun_check() {
                screen -dmS checkuser python3 /etc/SSHPlus/check.py $porta 1
                [[ $(grep -wc "check.py" /etc/autostart) = '0' ]] && {
                    echo -e "netstat -tlpn | grep -w $porta > /dev/null || {  screen -r -S 'ws' -X quit;  screen -dmS checkuser python3 /etc/SSHPlus/check.py $porta 1; }" >>/etc/autostart
                } || {
                    sed -i '/check.py/d' /etc/autostart
                    echo -e "netstat -tlpn | grep -w $porta > /dev/null || {  screen -r -S 'ws' -X quit;  screen -dmS checkuser python3 /etc/SSHPlus/check.py $porta 1; }" >>/etc/autostart
                }
                sleep 1
            }

            fun_check2() {
                screen -dmS checkuser python3 /etc/SSHPlus/check.py $porta 2
                [[ $(grep -wc "check.py" /etc/autostart) = '0' ]] && {
                    echo -e "netstat -tlpn | grep -w $porta > /dev/null || {  screen -r -S 'ws' -X quit;  screen -dmS checkuser python3 /etc/SSHPlus/check.py $porta 2; }" >>/etc/autostart
                } || {
                    sed -i '/check.py/d' /etc/autostart
                    echo -e "netstat -tlpn | grep -w $porta > /dev/null || {  screen -r -S 'ws' -X quit;  screen -dmS checkuser python3 /etc/SSHPlus/check.py $porta 2; }" >>/etc/autostart
                }
                sleep 1
            }
            echo ""
            echo -e "\033[1;32mQUAL FORMATO DESEJA ULTILIZAR? \033[1;33m?\033[1;37m: "
            echo -e "\033[1;31mFORMATO DA DATA PODE VARIAR DEPENDENDO DA PROGRAMAÇÃO DO SEU APLICATIVO.\033[0m"
            echo -e "\033[1;31mSELECIONE O FORMATO CONFORME A SUA NECESSIDADE.\033[0m"
            echo ""
            echo -e "\033[1;31m[\033[1;36m1\033[1;31m] \033[1;37m• \033[1;33mFORMATO YYYY/MM/DD (MAIS COMUN)\033[0m"
            echo -e "\033[1;31m[\033[1;36m2\033[1;31m] \033[1;37m• \033[1;33mFORMATO DD/MM/YYYY\033[0m"
            echo ""
            echo -ne "\033[1;36mOpção: \033[1;37m"
            read resposta
            if [[ "$resposta" = '1' ]]; then
                echo ""
                fun_bar 'fun_check'
            elif [[ "$resposta" = '2' ]]; then
                echo ""
                fun_bar 'fun_check2'
            else
                echo ""
                echo -e "\033[1;31mOpcao invalida !\033[0m"
                sleep 3
                fun_initcheck
            fi
            echo ""
            echo -e "\033[1;32mCHECKUSER(PADRÃO) ATIVADO COM SUCESSO\033[1;33m"
            echo ""
            #IP=$(cat /etc/IP)
            IP=$(curl -s icanhazip.com)
            echo "URL: http://$IP:$porta/checkUser"
            echo ""
            echo -ne "\033[1;36mPressione para continuar: \033[1;37m"
            read x
            fun_initcheck
        fi
    elif [[ "$resposta" = '2' ]]; then
        if ps x | grep -w 4gcheck | grep -v grep 1>/dev/null 2>/dev/null; then
            clear
            echo -e "\E[41;1;37m             CHECKUSER(CONECTA4G)              \E[0m"
            echo ""
            fun_stopbad() {
                screen -r -S "4gcheck" -X quit
                [[ $(grep -wc "4gcheck.py" /etc/autostart) != '0' ]] && {
                    sed -i '/4gcheck.py/d' /etc/autostart
                }
                sleep 1
                screen -wipe >/dev/null
            }
            echo -e "\033[1;32mDESATIVANDO O CHECKUSER(CONECTA4G)\033[1;33m"
            echo ""
            fun_stopbad
            echo ""
            echo -e "\033[1;32mCHECKUSER(CONECTA4G) DESATIVADO COM SUCESSO!\033[1;33m"
            sleep 1
            fun_initcheck
        else
            clear
            echo -e "\E[44;1;37m            ATIVAÇÃO DO CHECKUSER(CONECTA4G)             \E[0m"
            echo ""
            echo -ne "\033[1;32mQUAL PORTA DESEJA ULTILIZAR \033[1;33m?\033[1;37m: "
            read porta
            [[ $porta != ?(+|-)+([0-9]) ]] && {
                echo ""
                echo -e "\033[1;31mPorta invalida!"
                sleep 3
                clear
                fun_initcheck
            }
            verif_ptrs $porta

            fun_udpon() {
                screen -dmS 4gcheck python3 /etc/SSHPlus/4gcheck.py $porta
                [[ $(grep -wc "4gcheck.py" /etc/autostart) = '0' ]] && {
                    echo -e "netstat -tlpn | grep -w $porta > /dev/null || {  screen -r -S 'ws' -X quit;  screen -dmS checkuser python3 /etc/SSHPlus/4gcheck.py $porta; }" >>/etc/autostart
                } || {
                    sed -i '/check.py/d' /etc/autostart
                    echo -e "netstat -tlpn | grep -w $porta > /dev/null || {  screen -r -S 'ws' -X quit;  screen -dmS checkuser python3 /etc/SSHPlus/4gcheck.py $porta; }" >>/etc/autostart
                }
                sleep 1
            }
            echo ""
            fun_bar 'fun_udpon'
            echo ""
            echo -e "\033[1;32mCHECKUSER(PADRÃO) ATIVADO COM SUCESSO\033[1;33m"
            echo ""
            #IP=$(cat /etc/IP)
            IP=$(curl -s icanhazip.com)
            echo "URL: http://$IP:$porta/checkUser"
            echo ""
            echo -ne "\033[1;36mPressione para continuar: \033[1;37m"
            read x
            fun_initcheck
        fi
    elif [[ "$resposta" = '0' ]]; then
        echo ""
        echo -e "\033[1;31mRetornando...\033[0m"
        sleep 1
        menu
    else
        echo ""
        echo -e "\033[1;31mOpcao invalida !\033[0m"
        sleep 1
        fun_initcheck
    fi
}

inst_depedencias() {
    # ehck installed pip3
    if ! [ -x "$(command -v pip3)" ]; then
        echo 'Error: pip3 nÃ£o estÃ¡ instalado.' >&2
        echo 'Instale pip3 e execute o script novamente.' >&2

        if ! apt-get install -y python3-pip; then
            echo 'Erro ao instalar pip3' >&2
            exit 1
        else
            echo 'Instalado pip3 com sucesso'
        fi
    fi

    # install flask
    pip3 install flask >/dev/null 2>&1

    # download check.py
    [[ -e "/etc/SSHPlus/check.py" ]] && {
        sleep 0.1
    } || {
        cd $HOME
        wget https://notabug.org/guardvpn/Modderajuda/raw/main/M/check.py -o /dev/null
        mv -f $HOME/check.py /etc/SSHPlus/check.py
    }

    [[ -e "/etc/SSHPlus/4gcheck.py" ]] && {
        sleep 0.1
    } || {
        cd $HOME
        wget https://notabug.org/guardvpn/Modderajuda/raw/main/M/4gcheck.py -o /dev/null
        mv -f $HOME/4gcheck.py /etc/SSHPlus/4gcheck.py
    }

    [[ -e "/bin/check" ]] && {
        sleep 0.1
    } || {
        cd $HOME
        wget https://notabug.org/guardvpn/Modderajuda/raw/main/M/check -o /dev/null
        mv -f $HOME/check /bin/check
        chmod 777 /bin/check
    }
}

[[ -e "/etc/SSHPlus/check.py" ]] && [[ -e "/etc/SSHPlus/4gcheck.py" ]] && [[ -e "/bin/check" ]] && {
    fun_initcheck
} || {
    clear
    echo -e "\E[44;1;37m           INSTALADOR CHECKUSER             \E[0m"
    echo -e "\n\033[1;33mVC ESTA PRESTES A INSTALAR O CHECKUSER !\033[0m"
    echo ""
    echo -ne "\033[1;32mDESEJA CONTINUAR \033[1;31m? \033[1;33m[s/n]:\033[1;37m "
    read resposta
    [[ "$resposta" = 's' ]] && {
        clear
        echo -e "\E[41;1;37m             INSTALANDO CHECKUSER...              \E[0m"
        echo ""
        fun_bar 'inst_depedencias'
        echo -e "\033[1;32mCHECKUSER INSTALADO\033[1;33m"
        sleep 3
        fun_initcheck
    } || {
        echo -e "\n\033[1;31mRetornando...\033[0m"
        sleep 2
        clear
        menu
    }
}
